#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int q[N];
int cnt[N];
int find(int x)
{
    if(q[x]!=x)q[x]=find(q[x]);
    return q[x];
}

int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        q[i]=i;
        cnt[i]=1;
    }
    while (m -- )
    {
        string op;
        int a,b;
        cin>>op;
        if(op=="C")
        {
            cin>>a>>b;
            cnt[find(a)]+=cnt[find(b)];
            q[find(a)]=find(b);
        }
        else if(op=="Q1")
        {
            cin>>a>>b;
            if(find(a)==find(b))cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        else if(op=="Q2")
        {
            cin>>a;
            cout<<cnt[find(a)]<<endl;
        }
    }
    return 0;
}
